angular.module('ionicApp.controllers', [])

//登录
.controller('SignInCtrl', function($scope, $http, $state) {

	$scope.signIn = function(user) {
		$http.get(API_URl + 'UsersServlet?methodType=staffLogin&phoneNumber='+ user.phoneNumber+'&password='+ user.password).success(function(data){
		if(data.status.code == 0){
			console.log(data.data.name);
			alert('登录成功');
			$state.go('app.home');
			storage.setItem('phoneNumber',user.phoneNumber);
			storage.setItem('id',data.data.id);
			storage.setItem('name',data.data.name);
			storage.setItem('nickname',data.data.nickname);
			storage.setItem('user_type',data.data.user_type);
			storage.setItem('imgUrl',data.data.imgUrl);
			storage.setItem('imgMiniUrl',data.data.imgMiniUrl);
			storage.setItem('bio',data.data.bio);
			storage.setItem('email',data.data.email);
			storage.setItem('birthdate',data.data.birthdate);
			storage.setItem('created_at',data.data.created_at);
			storage.setItem('gender',data.data.gender);
			storage.setItem('homepage_switch',data.data.homepage_switch);
			storage.setItem('buzz_switch',data.data.buzz_switch);
		}else{
			alert(data.status.msg);
		}
		}).error(function(){
			alert("网络错误");
		});

		//console.log()
	};
})

//首页
.controller('HomeCtrl', function($scope,$ionicNavBarDelegate) {

})

//所有商户
.controller('AllBusinessCtrl', function($scope, $http, $ionicNavBarDelegate) {
//longitude=116.429&latitude=40.0407&
	$http.get(searchURL + '/business/detailCount.ac?size=10&range=5&sorttype=0&statisticsFields=business_category_id').success(function(data){
		$scope.all_business = data.data[0].result;
	}).error(function(status){
		alert("网络错误");
	});

	var myId = storage.getItem('id');
	$http.get(searchURL + '/business/detailCount.ac?size=10&range=5&sorttype=1&statisticsFields=business_category_id&factor=creator_id must '+myId).success(function(data){
		$scope.my_business = data.data[0].result;
	}).error(function(status){
		alert("网络错误");
	});

	console.log($scope.$$nextSibling,$scope.$$prevSibling);
})

//商户列表
.controller('SearchListCtrl', function($scope, $http, $stateParams) {
	$http.get(searchURL + '/business/keywordSearch.ac?longitude=116.429&latitude=40.0407&range=&from=0&size=100&sorttype=1&factor=business_category_id must '+$stateParams.business_category_id).success(function(data){
		console.log($stateParams.business_category_id,$stateParams.name,data.data[0].result[0].name);
		$scope.search_list = data.data[0].result;
	}).error(function(status){
		alert("网络错误");
	})
})

//优惠列表
.controller('PreferentialListCtrl', function($scope, $http) {
//longitude=116.429&latitude=40.0407&
	$http.get(searchURL + '/business/detailCount.ac?size=10&range=5&sorttype=0&statisticsFields=business_category_id').success(function(data){
		$scope.all_business = data.data[0].result;
	}
	).error(function(status){
			alert("网络错误");
	});
})


.controller('MerchantViewCtrl', function($scope, $http, $stateParams){
	$http.get(API_URl + "StaffBusinessServlet?methodType=getById&businessId="+$stateParams.id).success(function(data){
		console.log($stateParams.id,data.data);
		$scope.merchant = data.data;
	}
	).error(function(status){
		alert("网络错误");
	})
})

	.controller('TabsCtrl', ['$scope', function ($scope) {
		$scope.tabs = [{
			title: '所有商户',
			url: 'one.tpl.html'
		}, {
			title: '我的商户',
			url: 'two.tpl.html'
		}];

		$scope.currentTab = 'one.tpl.html';

		$scope.onClickTab = function (tab) {
			$scope.currentTab = tab.url;
		}

		$scope.isActiveTab = function(tabUrl) {
			return tabUrl == $scope.currentTab;
		}
	}]);